<?php
/**
 * Rights module tables
 * 
 * @package Database\Migrations
 */
class m110805_153437_installYiiUser extends CDbMigration {	

	protected $MySqlOptions = 'ENGINE=InnoDB CHARSET=utf8';
    private $_model;
    
	public function safeUp() {
        $this->createTable(Yii::app()->getModule('user')->tableUsers, array(
            "id" => "pk",
            "username" => "varchar(20) NOT NULL DEFAULT ''",
            "password" => "varchar(128) NOT NULL DEFAULT ''",
            "email" => "varchar(128) NOT NULL DEFAULT ''",
            "activkey" => "varchar(128) NOT NULL DEFAULT ''",
            "create_at" => "int(10) NOT NULL DEFAULT 0",
            "lastvisit_at" => "int(10) NOT NULL DEFAULT 0",
            "superuser" => "int(1) NOT NULL DEFAULT 0",
            "status" => "int(1) NOT NULL DEFAULT 0",
        ), $this->MySqlOptions);

        $this->createIndex('user_username', Yii::app()->getModule('user')->tableUsers, 'username', true);
        $this->createIndex('user_email', Yii::app()->getModule('user')->tableUsers, 'email', true);

        $this->createTable(Yii::app()->getModule('user')->tableProfiles, array(
            'user_id' => 'pk',
            'first_name' => 'string',
            'last_name' => 'string',
        ), $this->MySqlOptions);
		
        $this->addForeignKey('user_profile_id', Yii::app()->getModule('user')->tableProfiles, 'user_id', Yii::app()->getModule('user')->tableUsers, 'id', 'CASCADE', 'RESTRICT');

        $this->createTable(Yii::app()->getModule('user')->tableProfileFields, array(
            "id" => "pk",
            "varname" => "varchar(50) NOT NULL DEFAULT ''",
            "title" => "varchar(255) NOT NULL DEFAULT ''",
            "field_type" => "varchar(50) NOT NULL DEFAULT ''",
            "field_size" => "int(3) NOT NULL DEFAULT 0",
            "field_size_min" => "int(3) NOT NULL DEFAULT 0",
            "required" => "int(1) NOT NULL DEFAULT 0",
            "match" => "varchar(255) NOT NULL DEFAULT ''",
            "range" => "varchar(255) NOT NULL DEFAULT ''",
            "error_message" => "varchar(255) NOT NULL DEFAULT ''",
            "other_validator" => "text",
            "default" => "varchar(255) NOT NULL DEFAULT ''",
            "widget" => "varchar(255) NOT NULL DEFAULT ''",
            "widgetparams" => "text",
            "position" => "int(3) NOT NULL DEFAULT 0",
            "visible" => "int(1) NOT NULL DEFAULT 0",
        ), $this->MySqlOptions);
	}

	public function safeDown()
	{
        $this->dropTable(Yii::app()->getModule('user')->tableProfileFields);
        $this->dropTable(Yii::app()->getModule('user')->tableProfiles);
        $this->dropTable(Yii::app()->getModule('user')->tableUsers);
	}
}